package mysql

import (
	"gorm.io/gorm"
	"video-manager/usersrpc/usersrpc"
)

type Comment struct {
	gorm.Model
	Video_id        int    `gorm:"type:int(11);commit:'视频ID'"`
	User_id         int    `gorm:"type:int(11);commit:'用户ID'"`
	Comment_content string `gorm:"type:varchar(500);not null;commit:'评论内容'"`
	Is_anonymous    int8   `gorm:"type:tinyint;default:0;commit:'是否匿名'"`
	Comment_time    string `gorm:"type:varchar(300);commit:'评论时间'"`
	Comment_status  int8   `gorm:"type:tinyint;default:0;commit:'评论状态'"`
	Reply_id        int    `gorm:"type:int(11);commit:'回复ID'"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error

}

func (c *Comment) CommentById(db *gorm.DB, id int64) error {
	return db.Debug().Where("comments.id= ?", id).Find(&c).Error
}

func (c *Comment) CommentDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("comments.id= ?", id).Delete(&c).Error
}
func CommentList(db *gorm.DB, in *usersrpc.CommentListRequest, list []*usersrpc.CommentList) []*usersrpc.CommentList {
	db.Debug().Model(&Comment{}).Select("videos.video_title AS video_title",
		"users.nick_name AS nick_name",
		"comments.comment_content as comment_content").
		Joins("JOIN users ON comments.user_id = users.id").
		Joins("JOIN videos ON videos.id = comments.video_id").
		Order("comments.created_at DESC").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
func CommentLists(db *gorm.DB, in *usersrpc.CommentListRequest, list []*usersrpc.CommentList) []*usersrpc.CommentList {
	db.Debug().Model(&Comment{}).Select("videos.video_title AS video_title",
		"comments.comment_content as comment_content").
		Joins("JOIN videos ON videos.id = comments.video_id").
		Order("comments.created_at DESC").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
